package com.work.keep.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.work.keep.R;
import com.work.keep.activity.VideoActivity;
import com.work.keep.datas.SingleWorkoutData;
import com.work.keep.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/23.
 */
public class SingleWorkoutHorAdapter extends RecyclerView.Adapter<SingleWorkoutHorAdapter.MyViewHolder> {
    private Context context;
    private List<SingleWorkoutData.DataEntity.StepsEntity> stepsEntities;
    ImageLoader imageLoader;

    public SingleWorkoutHorAdapter(Context context) {
        this.context = context;
        stepsEntities = new ArrayList<>();
        imageLoader = VolleySingleton.getInstance()._getImageLoader();
    }

    public void addData(List<SingleWorkoutData.DataEntity.StepsEntity> stepsEntities) {
        this.stepsEntities = stepsEntities;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_single_workout_hor, parent, false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        if (stepsEntities.size() > 0) {
            holder.thumbNail.setDefaultImageResId(R.mipmap.photo);
            holder.thumbNail.setErrorImageResId(R.mipmap.photo);
            holder.thumbNail.setImageUrl(stepsEntities.get(position).getExercise().getVideos().get(1).getThumbnail(), imageLoader);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    VideoActivity.goToVideoActivity(context, stepsEntities, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return stepsEntities.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        NetworkImageView thumbNail;

        public MyViewHolder(View itemView) {
            super(itemView);
            thumbNail = (NetworkImageView) itemView.findViewById(R.id.thumbNail);
        }
    }
}
